﻿using SymulatorEthernetWPF.Shared;
using System;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace SymulatorEthernetWPF.SimObjects
{
    [Serializable]
    public class MacAdress :ISerializable
    {
        public String Adress;

        public MacAdress(String Adress)
        {
            this.Adress = CheckRegxp(Adress) ? Adress : null;

        }

         public MacAdress(SerializationInfo info, StreamingContext context)
        {
            Adress = (String)info.GetValue("adress", typeof(String));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("adress", Adress);
        }

        public bool SetMac(String Adress)
        {
            if (CheckRegxp(Adress))
            {
                this.Adress = Adress;
                return true;
            }
            return false;
        }

        public string GetMac()
        {
            return Adress;
        }

        public override string ToString()
        {
            return Adress;
        }

        private bool CheckRegxp(String Adress)
        {
            var r = new Regex(@"^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$");
            Match m = r.Match(Adress);
            if (m.Success)
            {
                return true;
            }
            return false;
        }

        public Boolean Equals(MacAdress obj)
        {
            return obj.Adress.Equals(Adress);
        }
    }
}